package com.duan.laugh.msg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duan.laugh.common.core.util.Query;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.common.security.util.UserUtils;
import com.duan.laugh.msg.api.pojo.entity.MessageLog;
import com.duan.laugh.msg.service.IMessageLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 消息日志 controller
 * </p>
 *
 * @author liutf
 * @since 2019-11-15
 */
@Api(tags = "消息日志")
@RestController
@RequestMapping("/message-log")
public class MessageLogController {
    @Autowired
    private IMessageLogService messageLogService;

    /**
     * 新增消息日志
     *
     * @param messageLog 实体
     * @return true/false
     */
    @PostMapping
    @ApiOperation("新增消息日志")
    public R save(@RequestBody MessageLog messageLog) {
        return R.ok(messageLogService.save(messageLog));
    }

    /**
     * 根据id查看消息日志
     *
     * @param id
     * @return MessageLog
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查看消息日志")
    public R<MessageLog> getById(@PathVariable String id) {
        return R.ok(messageLogService.getById(id));
    }

    /**
     * 分页查询消息日志
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @PostMapping("/page")
    @ApiOperation("分页查询消息日志")
    public IPage<MessageLog> page(@RequestBody Map<String, Object> params) {
        return messageLogService.page(new Query<>(params));
    }

    /**
     * 根据id删除消息日志
     *
     * @param id
     * @return true/false
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除消息日志")
    public R removeById(@PathVariable String id) {
        return R.ok(messageLogService.removeById(id));
    }

    /**
     * 根据id修改消息日志
     *
     * @param messageLog 实体
     * @return true/false
     */
    @PutMapping
    @ApiOperation("根据id修改消息日志")
    public R updateById(@RequestBody MessageLog messageLog) {
        return R.ok(messageLogService.updateById(messageLog));
    }

    /**
     * 0070106 分页查询我的消息记录
     * <p>
     * 发送人或接收人是登录人的消息
     *
     * @param params 分页参数
     * @return
     */
    @GetMapping("my")
    @ApiOperation("0070106 分页查询我的消息记录")
    public IPage<MessageLog> pageByMy(Map<String, Object> params) {
        // 登录人id
        final Long userId = UserUtils.getUser().getId();
        return messageLogService.page(new Query<>(params),
                new LambdaQueryWrapper<MessageLog>()
                        .eq(MessageLog::getReceivePersonId, userId).or().eq(MessageLog::getSendPersonId, userId)
                        .orderByAsc(MessageLog::getCreateTime));
    }
}

